*
* Run SP-DA with no dominated choices (state-funded on top)
*
* Inputs:
*	"${data_flip_${year}}"
*	"${data_drop_${year}}"
*	"${data_contract_dictionary_${year}}"
*	"${welfare_analysis_no_err_${year}}"
*
* Output:
*	"${match_no_dominated_output_${year}}"
*

* Correct dominated choices
use "${data_flip_${year}}", clear
append using "${data_drop_${year}}"
keep if eligibility == 1
	
replace rank = rank/100
merge n:1 program_id state_funded using "${data_contract_dictionary_${year}}", keepusing(contract_id)
keep if _m == 3
drop _m
	
append using "${welfare_analysis_no_err_${year}}", ///
	keep(id applicant_id rank contract_id state_funded program_id priority_score capacity)
	
sort id applicant_id contract_id rank
bys id applicant_id contract_id: gen counter = _n
keep if counter == 1
drop counter
	
bys program_id state_funded contract_id: egen capacity_max = max(capacity)
drop capacity sample_hss eligibility
rename capacity_max capacity
lab var capacity "Capacity"

* Run SP-DA
do "${helpers_dir}/runSPDA.do"
	
	*preserve
	*	collapse (count) num_admitted_upper = id (min) priority_score_spda capacity_spda, by(contract_id_spda state_funded_spda program_id_spda)
	*	rename state_funded_spda state_funded
	*	rename program_id_spda program_id
	*	rename priority_score_spda priority_score_upper
	*	rename capacity_spda capacity_upper
	*	drop contract_id_spda
	*	save "${data_dir}/PriorityScoreCutoffsUpper_${year}.dta", replace
	*restore
drop priority_score_spda capacity_spda

rename contract_id_spda contract_id_spda_cf
lab var contract_id_spda_cf "Contract id (no dominated choice, state-funded on top)"

rename rank_spda rank_spda_cf
lab var rank_spda_cf "Rank on ROL (no dominated choice, state-funded on top)"

rename state_funded_spda state_funded_spda_cf
lab var state_funded_spda_cf "State-funded (no dominated choice, state-funded on top)"

rename program_id_spda program_id_spda_cf
lab var program_id_spda_cf "Program id (no dominated choice, state-funded on top)"
	
save "${match_no_dominated_upper_${year}}", replace
